package cn.com.zte.ant.task.doc.nj.excel.tools;

import cn.com.zte.ngbc.frw.model.tran.error.global.ErrorConf;
import cn.com.zte.ngbc.frw.model.tran.error.global.ErrorConf.ErrorType;
import cn.com.zte.ngbc.frw.model.tran.error.global.ErrorConfs;
import cn.com.zte.ngbc.frw.model.tran.error.global.ErrorParameter;
import cn.com.zte.ngbc.frw.model.tran.error.global.GlobalErrorConf;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class ErrorCodeExcelProcessor
  extends AbstractExcelProcessor<GlobalErrorConf>
{
  public HSSFWorkbook process(List<GlobalErrorConf> models, File fileName)
  {
    HSSFWorkbook book = new HSSFWorkbook();
    String name = fileName.getName().substring(0, fileName.getName().indexOf("."));
    HSSFSheet overView = book.createSheet(name + "错误码总览");
    
    short s = Short.parseShort("1");
    HSSFFont font = book.createFont();
    font.setUnderline(UNDERLINE);
    font.setColor((short)12);
    
    HSSFFont font2 = book.createFont();
    font2.setBoldweight((short)700);
    
    HSSFCellStyle style = book.createCellStyle();
    style.setBorderLeft(s);
    style.setBorderBottom(s);
    style.setBorderRight(s);
    style.setBorderTop(s);
    
    HSSFCellStyle style3 = book.createCellStyle();
    style3.setBorderLeft(s);
    style3.setBorderBottom(s);
    style3.setBorderRight(s);
    style3.setBorderTop(s);
    style3.setFillPattern(this.FILLPATTERN);
    style3.setFillForegroundColor(this.FGC_GREY_25);
    
    HSSFCellStyle style2 = book.createCellStyle();
    style2.setBorderLeft(s);
    style2.setBorderBottom(s);
    style2.setBorderRight(s);
    style2.setBorderTop(s);
    style2.setFont(font);
    
    HSSFRow title = overView.createRow(0);
    HSSFCell t_cell0 = title.createCell(0);
    t_cell0.setCellValue("类别");
    t_cell0.setCellStyle(style3);
    HSSFCell t_cell1 = title.createCell(1);
    t_cell1.setCellValue("错误码类型");
    t_cell1.setCellStyle(style3);
    HSSFCell t_cell2 = title.createCell(2);
    t_cell2.setCellValue("中文名称");
    t_cell2.setCellStyle(style3);
    HSSFCell t_cell3 = title.createCell(3);
    t_cell3.setCellValue("错误码类型");
    t_cell3.setCellStyle(style3);
    HSSFCell t_cell4 = title.createCell(4);
    t_cell4.setCellValue("中文名称");
    t_cell4.setCellStyle(style3);
    
    HSSFCellStyle cata = book.createCellStyle();
    cata.setWrapText(true);
    cata.setAlignment((short)2);
    cata.setVerticalAlignment((short)1);
    cata.setFont(font2);
    for (GlobalErrorConf gec : models)
    {
      HSSFRow blank = overView.createRow(overView.getLastRowNum() + 1);
      blank.createCell(0);
      blank.createCell(1);
      blank.createCell(2);
      blank.createCell(3);
      blank.createCell(4);
      
      List<ErrorConfs> ecs = gec.getErrors();
      if ((ecs != null) && (ecs.size() != 0))
      {
        List<ErrorConfs> team1 = ecs.subList(0, ecs.size() / 2 + ecs.size() % 2);
        List<ErrorConfs> team2 = null;
        if (ecs.size() > 1) {
          team2 = ecs.subList(ecs.size() / 2 + ecs.size() % 2, ecs.size());
        }
        int start = overView.getLastRowNum() + 1;
        for (int i = 0; i < team1.size(); i++)
        {
          int lastnm = overView.getLastRowNum() + 1;
          HSSFRow row = overView.createRow(lastnm);
          HSSFCell catagory = row.createCell(0);
          if (i == 0) {
            catagory.setCellValue(gec.getLongname());
          }
          catagory.setCellStyle(cata);
          
          HSSFCell cellName1 = row.createCell(1);
          cellName1.setCellValue(((ErrorConfs)team1.get(i)).getId());
          cellName1.setCellStyle(style2);
          
          HSSFCell cellLname2 = row.createCell(2);
          cellLname2.setCellValue(((ErrorConfs)team1.get(i)).getLongname());
          cellLname2.setCellStyle(style);
          
          HSSFCell cellName3 = row.createCell(3);
          cellName3.setCellStyle(style2);
          if ((team2 != null) && (i <= team2.size() - 1)) {
            cellName3.setCellValue(((ErrorConfs)team2.get(i)).getId());
          }
          HSSFCell cellLname4 = row.createCell(4);
          cellLname4.setCellStyle(style);
          if ((team2 != null) && (i <= team2.size() - 1)) {
            cellLname4.setCellValue(((ErrorConfs)team2.get(i)).getLongname());
          }
        }
        int end = overView.getLastRowNum();
//        overView.addMergedRegion(new CellRangeAddress(start, end, 0, 0));
      }
    }
    for (int i = 0; i < 4; i++) {
      overView.autoSizeColumn(i);
    }
    for (GlobalErrorConf gec : models) {
      if ((gec.getErrors() != null) && (gec.getErrors().size() != 0) && 
      
        (book.getSheet(gec.getLongname()) == null))
      {
        HSSFSheet sheet = book.createSheet(gec.getLongname());
        List<ErrorConfs> ecss = gec.getErrors();
       
        
        List<HSSFCellModel[]> contents = new ArrayList();
        for (ErrorConfs ecs : ecss)
        {
          contents.add(new HSSFCellModel[] { new HSSFCellModel("错误类型名称:", Short.valueOf(this.ALIGN_LEFT), Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_BLUE_GREY)), new HSSFCellModel(ecs.getLongname(), Short.valueOf(this.ALIGN_LEFT), Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_BLUE_GREY)), new HSSFCellModel("错误类型", Short.valueOf(this.ALIGN_LEFT), Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_BLUE_GREY)), new HSSFCellModel(ecs.getId(), Short.valueOf(this.ALIGN_LEFT), Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_BLUE_GREY)), new HSSFCellModel("NULL", null, Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_BLUE_GREY)), new HSSFCellModel("NULL", null, Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_BLUE_GREY)), new HSSFCellModel("NULL", null, Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_BLUE_GREY)), new HSSFCellModel("NULL", null, Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_BLUE_GREY)) });
          System.out.println(gec.getId()+"."+ecs.getId());
          contents.add(new HSSFCellModel[] { new HSSFCellModel("错误码定义明细:", Short.valueOf(this.ALIGN_LEFT), Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_GREY_25)), new HSSFCellModel("NULL", null, Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_GREY_25)), new HSSFCellModel("NULL", null, Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_GREY_25)), new HSSFCellModel("NULL", null, Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_GREY_25)), new HSSFCellModel("NULL", null, Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_GREY_25)), new HSSFCellModel("NULL", null, Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_GREY_25)), new HSSFCellModel("NULL", null, Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_GREY_25)), new HSSFCellModel("NULL", null, Short.valueOf(this.FILLPATTERN), Short.valueOf(this.FGC_GREY_25)) });
          for (ErrorConf ec : ecs.getErrors())
          {
        	  
           // contents.add(new HSSFCellModel[] { new HSSFCellModel("错误码", Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel(ec.getId(), Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel("错误级别", Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel(ec.getType().getValue(), Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel("错误描述:", Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel(ec.getMessage(), Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel("错误ID", Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel(ec.getErrorCode(), Short.valueOf(this.ALIGN_LEFT), null, null) });
            
            contents.add(new HSSFCellModel[] { new HSSFCellModel("参数列表:", Short.valueOf(this.ALIGN_LEFT), null, null, new FontModel(null, null, new Short("220"), Short.valueOf(this.FONT_BORDER), null, null)), new HSSFCellModel("NULL", null, null, null), new HSSFCellModel("NULL", null, null, null), new HSSFCellModel("NULL", null, null, null), new HSSFCellModel("NULL", null, null, null), new HSSFCellModel("NULL", null, null, null), new HSSFCellModel("NULL", null, null, null), new HSSFCellModel("NULL", null, null, null) });
            if ((ec.getParameters() != null) && (ec.getParameters().size() > 0)) {
              for (ErrorParameter ep : ec.getParameters()) {
                contents.add(new HSSFCellModel[] { new HSSFCellModel("参数ID", Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel(ep.getId(), Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel("参数名称", Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel(ep.getLongname(), Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel("参数类型", Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel(ep.getType(), Short.valueOf(this.ALIGN_LEFT), null, null), new HSSFCellModel("NULL", null, null, null), new HSSFCellModel("NULL", null, null, null) });
              }
            }
          }
        }
     //   createRow(contents, sheet);
        for (int i = 0; i < 8; i++) {
          sheet.autoSizeColumn(i);
        }
      }
    }
    int rows = overView.getLastRowNum();
    String sheetName = null;
    for (int j = 0; j <= rows; j++)
    {
      HSSFRow row = overView.getRow(j);
      if (row != null)
      {
        HSSFCell cell = row.getCell(0);
        if ((cell != null) && (cell.getStringCellValue() != null)) {
          sheetName = cell.getStringCellValue().trim();
        }
        if (sheetName != null)
        {
          HSSFSheet desctSheet = book.getSheet(sheetName);
          if (desctSheet != null)
          {
            HSSFCell cell1 = row.getCell(1);
            if (cell1 != null) {
              createLink(cell, cell.getStringCellValue(), desctSheet, 3);
            }
            HSSFCell cell3 = row.getCell(3);
            if (cell3 != null) {
              createLink(cell3, cell3.getStringCellValue(), desctSheet, 3);
            }
          }
        }
      }
    }
    writeFile(fileName.getAbsolutePath(), book);
    return book;
  }
}